(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:PintuanCtrl
     * @description
     * # PintuanCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('PintuanCtrl', PintuanCtrl);

    PintuanCtrl.$inject = ['$scope', 'dataService', 'helper', '$rootScope', '$location', 'sessionService'];

    function PintuanCtrl($scope, dataService, helper, $rootScope, $location, sessionService) {

        //打折活动列表
        $scope.listAction = function() {

            $scope.pageSize = 10;
            $scope.getList(1);
            $scope.$on('event:up-store-id', function() {
                $scope.getList(1);
            });

        }

        $scope.getList = function(pageNo) {
            var query = {};
            query.content = {};

            query.content.page_no = pageNo;
            query.content.page_size = $scope.page_Size ? $scope.page_Size : 10;
            query.content.store_id = $rootScope.store_id;

            if ($scope.keyword) {
                query.content.item_id = $scope.keyword;
            }

            //console.error("query-->", query);
            dataService.pintuanGet(query).then(function(list) {
                if (list) {
                    for (var i in list.pintuan_list) {
                        list.pintuan_list[i].diff_time = $scope.diff_time(list.pintuan_list[i].start_time, list.pintuan_list[i].end_time)
                    }
                    $scope.pintuan_list = list.pintuan_list;
                    $scope.total_count = list.total_count;
                }
            });
        }

        //js计算两个时间的时间差
        $scope.diff_time = function(start_time, end_time) {
            var s_time = new Date().getTime();
            var start = new Date(start_time).getTime();
            var end = new Date(end_time).getTime();

            //当前时间是否大于活动结束时间
            if (s_time > end) {
                return 0;
            }

            //判断当前时间是否大于开始时间
            if (s_time > start && s_time < end) {
                var time = new Date().getTime();
            } else {
                var time = new Date(start_time).getTime();
            }

            //var diff = end_time * 1000 - start_time * 1000 //时间差的毫秒数  
            var diff = end - time;

            //计算出相差天数  
            var days = Math.floor(diff / (24 * 3600 * 1000));

            //计算出小时数  
            var leave1 = diff % (24 * 3600 * 1000); //计算天数后剩余的毫秒数  
            var hours = Math.floor(leave1 / (3600 * 1000));
            //计算相差分钟数  
            var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数  
            var minutes = Math.floor(leave2 / (60 * 1000));

            //计算相差秒数  
            var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数  
            var seconds = Math.round(leave3 / 1000);

            var returnStr = seconds + "秒";
            if (minutes > 0) {
                returnStr = minutes + "分" + returnStr;
            }
            if (hours > 0) {
                returnStr = hours + "小时" + returnStr;
            }
            if (days > 0) {
                returnStr = days + "天" + returnStr;
            }
            return returnStr;
        }

        //拼团添加页面
        $scope.addAction = function() {
            $scope.pintuan_id = $location.search().pintuan_id;

            if ($scope.pintuan_id) {
                getOneData();
                $scope.$on('event:up-store-id', function() {
                    getOneData();
                });
            } else {
                $scope.pintuan_data = {
                    "store_id": $rootScope.store_id,
                    "name": "拼团活动",
                    "need_customer_num": 2, //总需要多少人团购 
                    "original_price": 100, //原价
                    "pin_price": 1, //拼团价格
                    "start_time": $scope.getDateStr(0),
                    "end_time": $scope.getDateStr(7),
                    "expiration_hour": 1,
                };

            }
        }



        function getOneData() {
            var query = {
                content: {
                    pintuan_id: $scope.pintuan_id,
                    store_id: $rootScope.store_id,
                    // item_id: $rootScope.store_id,
                    // sku_id: $rootScope.store_id,
                }
            };

            // console.error("$rootScope", $rootScope);
            dataService.pintuanGet(query).then(function(result) {
                if (result.pintuan_list) {
                    $scope.pintuan_data = result.pintuan_list[0];
                    $scope.pintuan_data.original_price = Number($scope.pintuan_data.original_price);
                    $scope.pintuan_data.pin_price = Number($scope.pintuan_data.pin_price);
                    $scope.pintuan_data.need_customer_num = Number($scope.pintuan_data.need_customer_num);
                    $scope.pintuan_data.expiration_hour = $scope.pintuan_data.expiration_hour ? $scope.pintuan_data.expiration_hour : "1";
                    $scope.oldsku_price = $scope.pintuan_data.original_price;
                    var item_id = $scope.pintuan_data.item_id;
                    var sku_id = $scope.pintuan_data.sku_id;
                    dataService.itemSkuOne(item_id, sku_id).then(function(item_data) {
                        var a = clone(item_data);
                        a.sku_data = item_data.sku;
                        $scope.pintuan_data.original_price = Number(a.sku_data.sku_price);
                        $scope.checked_item = a;
                    });
                }

            });
        }
        //添加页面
        $scope.infoAction = function() {
            $scope.pintuan_id = $location.search().pintuan_id;
            $scope.infoGet(1);
            $scope.$on('event:up-store-id', function() {
                $scope.infoGet(1);
            });
        }

        $scope.detilinfoAction = function() {
            $scope.pintuan_activity_id = $location.search().pintuan_activity_id;
            // $scope.num = $location.search().num;
            console.error(" $scope.num", $scope.num);
            $scope.detilinfoGet(1);
            $scope.$on('event:up-store-id', function() {
                $scope.detilinfoGet(1);
            });
        }

        $scope.detilinfoGet = function(pageNo) {
            var data = {
                content: {
                    page_no: pageNo,
                    page_size: $scope.page_Size ? $scope.page_Size : 10,
                }
            };

            if ($scope.pintuan_activity_id) {
                data.content.pintuan_activity_id = $scope.pintuan_activity_id;
            }

            if ($scope.pintuan_detail_id) {
                data.content.pintuan_detail_id = $scope.pintuan_detail_id;
            }

            dataService.pintuandetilGet(data).then(function(data) {
                if ($scope.pintuan_detail_id) {
                    $scope.detailData = data.pintuan_detail_list[0];
                } else {
                    $scope.detailDatas = data.pintuan_detail_list;
                    $scope.total_count = data.total_count;
                }

            });
        }

        $scope.detailoneAction = function() {
            $scope.pintuan_activity_id = $location.search().pintuan_activity_id;
            $scope.detilinfoGet(1);
            $scope.$on('event:up-store-id', function() {
                $scope.detilinfoGet(1);
            });
        }

        $scope.infoGet = function(pageNo) {
            var data = {
                content: {
                    page_no: pageNo,
                    page_size: $scope.page_Size ? $scope.page_Size : 10,
                    pintuan_id: $scope.pintuan_id,
                    store_id: $rootScope.store_id,
                    // order_by: "created_time:desc"
                    // pintuan_activity_id:"8",
                    // sku_id: "143446078283321344",
                }
            };

            dataService.pintuanactivityGet(data).then(function(data) {
                $scope.acttivityData = data.pintuanactivity_list;
                $scope.total_count = data.total_count;
            });
        }

        //活动添加
        $scope.pintuanAdd = function() {

            if (!$scope.pintuan_data.expiration_hour) {
                helper.myAlert("过期时间不能为空");
                return false;
            }

            if ($scope.pintuan_data.end_time < $scope.pintuan_data.start_time) {
                helper.myAlert("活动结束时间不能小于活动开始时间");
                return false;
            }
            if ($scope.pintuan_data.need_customer_num <= 1 || !$scope.pintuan_data.need_customer_num) {
                helper.myAlert("拼团人数最少两个人");
                return false;
            }

            if (!$scope.pintuan_data.original_price) {
                helper.myAlert("商品原价不能为空！");
                return false;
            }

            if ($scope.pintuan_data.pin_price > $scope.pintuan_data.original_price) {
                helper.myAlert("拼团价格不能大于原价");
                return false;
            }

            if ($scope.pintuan_data.pin_price <= 0 || !$scope.pintuan_data.pin_price) {
                helper.myAlert("拼团价格不能为空！不能为负值！");
                return false;
            }

            console.error("$scope.pintuan_id", $scope.pintuan_data);
            // return;
            if ($scope.pintuan_id) {
                dataService.pintuanUpdate($scope.pintuan_data).then(function(result) {
                    if (result.pintuan_id) {
                        helper.myAlert("success", "#/pintuan/list");
                    } else {
                        helper.myAlert(result.errmsg);
                    }
                });
            } else {
                dataService.pintuanAdd($scope.pintuan_data).then(function(result) {
                    if (result.pintuan_id) {
                        helper.myAlert("success", "#/pintuan/list");
                    } else {
                        helper.myAlert(result.Msg);
                    }
                });
            }
        }


        //砍价活动删除
        $scope.kanjiaDelete = function(bargain_rule_id) {
            helper.myConfirm('确认删除吗？', function() {
                var data = { 'bargain_rule_id': bargain_rule_id };
                console.error("删除--->", data);
                // return false;
                dataService.kanjiaDelete(data).then(function(result) {
                    console.error("删除结果：", result);
                    // return false;
                    if (result.bargain_rule_id) {
                        $scope.getList(1);
                    } else {
                        helper.myAlert("error:" + result);
                    }
                });
            });
        }

        //活动时间计算
        $scope.getDateStr = function(addDayCount) {
            var dd = new Date();
            dd.setDate(dd.getDate() + addDayCount);
            var y = dd.getFullYear();
            var m = (dd.getMonth() + 1) < 10 ? "0" + (dd.getMonth() + 1) : (dd.getMonth() + 1);
            var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
            var h = dd.getHours();
            var i = dd.getMinutes();
            var s = dd.getSeconds();

            if (i < 10) {
                i = "0" + i;
            }
            var s = dd.getSeconds();
            if (s < 10) {
                s = "0" + s;
            }

            return y + "-" + m + "-" + d + " " + h + ":" + i + ":" + s;
        }

        //商品directives回调方法
        $scope.itemdata = function(items) {
            //商品信息是多维数组时
            if (items) {
                //选中商品的item_id sku_id
                $scope.pintuan_data.item_id = items.sku_data.item_id;
                $scope.pintuan_data.sku_id = items.sku_data.sku_id;
                $scope.pintuan_data.original_price = Number(items.sku_data.sku_price);
                $scope.pintuan_data.pin_price = Number(items.sku_data.sku_price);
                //选中商品
                $scope.checked_item = items;

                // console.error("数据--->", $scope.pintuan_data);
            }

        }
    }
})();